define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    //增加题目
    $(document).on("click",".publish",function(){
        var course_id = $("#course_name option:selected").attr("data-id");
        var course_name = $("#course_name option:selected").val();
        var type = $("#category option:selected").attr("data-id");
        var title = $("#content").val();
        var txt = document.getElementsByName("txt").length;
        var sum = 0;
        var options = "[";
        for(var i=0;i<txt;i++){
            var option = $("#txt"+i).val();
            var answer = $('input:checkbox[id='+i+']:checked').attr("id");
            if(option!=undefined)
                options+= "{\"content\":\""+option+"\"";
            if(answer&&i<txt-1){
                options+= ",\"correct\":1},";
                sum++;
            }else if(answer&&i==txt-1){
                options+= ",\"correct\":1}]";
                sum++;
            }else if(!answer&&i<txt-1){
                options+= ",\"correct\":0},";
            }else{
                options+= ",\"correct\":0}]";
            }
        }
        var param = {};
        if(course_id){
            param.course_name = course_name;
            param.course_id = course_id;
        }else{
            alert("请选择题目所属课程~");
            return;
        }
        if(type){
            param.type = type;
        }else{
            alert("请选择题目类型~");
            return;
        }
        if(title){
            param.title = title;
        }else{
            alert("请输入题目详情~");
            return;
        }
        if(options==''){
            alert("请填写选项~");
            return;
        }else if(sum==0){
            alert("请选择正确选项~")
            return;
        }else{
            param.options = options;
        }
        if(sum>1&&type<=1){
            alert("答案数与题型不匹配~")
            return;
        }
        if(param){
            bootbox.confirm("确定要添加该题目？",function(result){
                if(result) {
                    IO.post("act/addquestion",param,function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/pte/question';
                            }
                        })
                    });
                }
            });
        }
    });


});
